<?php
/**
 * 后台Modal Widget
 * User: ZFC
 * Date: 2015/7/6
 * Time: 11:56
 */
//todo 需要再精简

namespace common\widgets;

use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class Modal extends Widget {

    //模态对话框标题
    public $title;

    //提交按钮
    public $submitButton;

    //取消按钮
    public $cancelButton;

    /*
     * 类型
     * 专为后台定做，目前类型有：view, add, edit, delete
     * 当没有指定id时，id根据type为 viewModal, addModal, editModal, deleteModal
     */
    public $type;

    /*
     * 模板
     * 默认：default; 查看：view; 编辑：edit; 删除：delete;
     */
    public $template = 'default';

    public $header;

    public $headerOptions;

    public $footer;

    public $size;

    public $footerOptions;

    public $closeButton = [];

    public $toggleButton = false;


    public function init()
    {
        //如果Modal的ID没有定义，则根据type设置ID
        if(strlen($this->id)<3 && !empty($this->type))
            $this->id = $this->type . 'Modal';

        parent::init();

        $this->initOptions();

        echo $this->renderToggleButton() . "\n";
        echo Html::beginTag('div', $this->options) . "\n";
        echo Html::beginTag('div', ['class' => 'modal-dialog ' . $this->size]) . "\n";
        echo Html::beginTag('div', ['class' => 'modal-content']) . "\n";
        echo $this->renderHeader() . "\n";
        echo $this->renderBodyBegin() . "\n";
    }

    /**
     * Renders the widget.
     */
    public function run()
    {
        echo "\n" . $this->renderBodyEnd();
        if(!in_array($this->type, ['edit', 'add']) )
            echo "\n" . $this->renderFooter();
        echo "\n" . Html::endTag('div'); // modal-content
        echo "\n" . Html::endTag('div'); // modal-dialog
        echo "\n" . Html::endTag('div');

        $this->registerPlugin('modal');
    }

    /**
     * Renders the header HTML markup of the modal
     * @return string the rendering result
     */
    protected function renderHeader()
    {
        $button = $this->renderCloseButton();
        if($this->title !== null) {
            $this->header = "<h3  class='modal-title'>" . $this->title;
        }
        if ($button !== null) {
            $this->header = $button . "\n" . $this->header ;
        }
        if ($this->header !== null) {
            Html::addCssClass($this->headerOptions, 'modal-header');
            return Html::tag('div', "\n" . $this->header . "\n", $this->headerOptions);
        } else {
            return null;
        }
    }

    /**
     * Renders the opening tag of the modal body.
     * @return string the rendering result
     */
    protected function renderBodyBegin()
    {
        return Html::beginTag('div', ['class' => 'modal-body']);
    }

    /**
     * Renders the closing tag of the modal body.
     * @return string the rendering result
     */
    protected function renderBodyEnd()
    {
        return Html::endTag('div');
    }

    /**
     * Renders the HTML markup for the footer of the modal
     * @return string the rendering result
     */
    protected function renderFooter()
    {
        if ($this->footer !== null) {

            Html::addCssClass($this->footerOptions, 'modal-footer');
            return Html::tag('div', "\n" . $this->footer . "\n", $this->footerOptions);
        } else {
            if($this->type == 'delete'){
                $this->footer = "<button type='button' class='btn btn-outline pull-left' data-dismiss='modal'>关闭</button>"
                    . "<a href='' class='btn btn-outline btn-ok'>确定</a>";
            }elseif($this->submitButton != ""){

                $this->footer = "<button type='button' class='btn btn-default pull-left' data-dismiss='modal'>关闭</button>"
                    . "<button type='button' class='btn btn-primary'>" . $this->submitButton . "</button>";
            }else{
                $this->footer = "<button type='button' class='btn btn-default pull-left' data-dismiss='modal'>关闭</button>";
            }
            Html::addCssClass($this->footerOptions, 'modal-footer');
            return Html::tag('div', "\n" . $this->footer . "\n", $this->footerOptions);
        }
    }

    /**
     * Renders the toggle button.
     * @return string the rendering result
     */
    protected function renderToggleButton()
    {
        if (($toggleButton = $this->toggleButton) !== false) {
            $tag = ArrayHelper::remove($toggleButton, 'tag', 'button');
            $label = ArrayHelper::remove($toggleButton, 'label', 'Show');
            if ($tag === 'button' && !isset($toggleButton['type'])) {
                $toggleButton['type'] = 'button';
            }

            return Html::tag($tag, $label, $toggleButton);
        } else {
            return null;
        }
    }

    /**
     * Renders the close button.
     * @return string the rendering result
     */
    protected function renderCloseButton()
    {
        if (($closeButton = $this->closeButton) !== false) {
            $tag = ArrayHelper::remove($closeButton, 'tag', 'button');
            $label = ArrayHelper::remove($closeButton, 'label', '&times;');
            if ($tag === 'button' && !isset($closeButton['type'])) {
                $closeButton['type'] = 'button';
            }

            return Html::tag($tag, $label, $closeButton);
        } else {
            return null;
        }
    }

    /**
     * Initializes the widget options.
     * This method sets the default values for various options.
     */
    protected function initOptions()
    {
        $this->options = array_merge([
            'class' => 'fade',
            'role' => 'dialog',
            'tabindex' => -1,
        ], $this->options);
        Html::addCssClass($this->options, 'modal');

        if ($this->clientOptions !== false) {
            $this->clientOptions = array_merge(['show' => false], $this->clientOptions);
        }

        if ($this->closeButton !== false) {
            $this->closeButton = array_merge([
                'data-dismiss' => 'modal',
                'aria-hidden' => 'true',
                'class' => 'close',
            ], $this->closeButton);
        }

        if ($this->toggleButton !== false) {
            $this->toggleButton = array_merge([
                'data-toggle' => 'modal',
            ], $this->toggleButton);
            if (!isset($this->toggleButton['data-target']) && !isset($this->toggleButton['href'])) {
                $this->toggleButton['data-target'] = '#' . $this->options['id'];
            }
        }
    }
}